Thread: [HELP] do-while loop

  1. #1
    Registered User hustlerism's Avatar
    Join Date
    Dec 2010
    Posts
    5

    [HELP] do-while loop

    I've been doing some C++ project n i encountered some problems. my program doesnt want to loop.



    #include<stdio.h>

    int main()
    {
    int price,depreciation,x=1,year,acc_depreciation,end_v alue;
    char ans;

    do{
    printf("The price of the machine (RM):");
    scanf("%d",&price);
    printf("The rate of depreciation (RM):");
    scanf("%d",&depreciation);
    printf("Years of depreciation :");
    scanf("%d",&year);

    end_value=price-(year*depreciation);
    acc_depreciation=price-end_value;


    printf("YEAR DEPRECIATION END-YEAR-VALUE ACCUMULATED DEPRECIATION\n");
    printf("---- ------------ -------------- ------------------------\n");

    for(x=1;x<=year;x++)
    {
    printf("%d %d %d %d\n", x,depreciation,price-(x*depreciation),x*depreciation);
    }


    {
    if(end_value<price/2)
    printf("The final end-year-value (RM %d) is less than half of the actual price (RM %d).\n",end_value,price/2);
    else if(end_value>price/2)
    printf("The final end-year-value (RM %d) is greater than half of the actual price (RM %d).\n",end_value,price/2);
    else
    printf("The final end-year-value (RM %d) is equals to half of the actual price (RM %d).\n",end_value,price/2);
    }


    printf("Do you want to continue? Y-yes,N-no: ");
    fflush(stdin);
    scanf("%c\n",&ans);
    }while(ans=='Y');




    system("pause");
    return 0;
    }
    Last edited by hustlerism; 12-12-2010 at 06:16 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    << !! Posting Code? Read this First !! >>
    Read it, edit your post for readable code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hustlerism's Avatar
    Join Date
    Dec 2010
    Posts
    5
    ive edited the codes. sry im new here n i need some help pls.
    *sigh*

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    i don understand what you want to mean in below code. it seem syntax problem.

    Code:
    {
    if(end_value<price/2)
    printf("The final end-year-value (RM %d) is less than half of the actual price (RM %d).\n",end_value,price/2);
    else if(end_value>price/2)
    printf("The final end-year-value (RM %d) is greater than half of the actual price (RM %d).\n",end_value,price/2);
    else
    printf("The final end-year-value (RM %d) is equals to half of the actual price (RM %d).\n",end_value,price/2);
    }

  5. #5
    Registered User hustlerism's Avatar
    Join Date
    Dec 2010
    Posts
    5
    idk how to explain cuz im a new programming student. but if u try the codes that ive given in Dev C++ u will know. RM is the currency of my country Malaysia. after key-in all the information needed it will show a table of calculated values. n then lastly "Do you want to continue?" n if i type Y it doesnt continue. thats the problem im facing right now.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    you need to change your
    scanf("%c\n",&ans);
    to
    scanf("\n%c",&ans);

  7. #7
    Registered User hustlerism's Avatar
    Join Date
    Dec 2010
    Posts
    5
    Quote Originally Posted by hugoguan View Post
    you need to change your
    scanf("%c\n",&ans);
    to
    scanf("\n%c",&ans);
    Hey thx. the problem solved. thx very much. i rly appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM